#!/bin/bash
#
# delpage
#
# Page deletion helper for the Dragora GNU/Linux-Libre website
# (https://www.dragora.org)
#
#
# Copyright (C) 2021 Michael Siegel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#### INCLUDES ####

. include/constants || exit 1
. include/subroutines || exit 1


#### CONSTANTS ####


#### GLOBAL PARAMETERS ####

target=


#### FUNCTIONS ####

## Retrievers

## User queries and input parsers

_prompt_target() {

  local input=

  while true
  do
    if [[ -z "$input" ]]
    then
      printf '%s\n' 'Please select a page to delete:'
      _show_pagetree -del
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    fi

    read -rp "$PROMPT" input

    if [[ -z "$input" ]]
    then
      continue
    elif [[ "$input" = "$HELP_COMMAND" ]]
    then
      _show_help "$FUNCNAME"
    elif [[ ! "$input" =~ ^[0123456789]+$ ]]
    then
      _perr "invalid selection -- '$input'"
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    else
      if [[ ! "$input" -gt 0 || ! "$input" -lt "${#pagetree[@]}" ]]
      then
        _perr "invalid selection -- '$input'"
        printf '%s\n' "$HELP_TIP_INTERACTIVE"
      else
        break
      fi
    fi
  done

  target="${pagetree[$input]%/}"
}

_prompt_confirm() {

  local input=
  
  while true
  do
    if [[ -z "$input" ]]
    then
      printf '%s\n' "Going to remove '$target'. Are you sure? [y/n]"
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    fi

    read -rp "$PROMPT" input

    if [[ -z "$input" ]]
    then
      continue
    elif [[ "$input" = "$HELP_COMMAND" ]]
    then
      _show_help "$FUNCNAME"
    elif [[ ! "$input" =~ ^y|n$ ]]
    then
      _perr "invalid input -- '$input'"
    else
      break
    fi
  done

  if [[ "$input" = n ]]
  then
    return 1
  else
    return 0
  fi
}

## Checks

## Actions

_del_page() {

  local target_full=
  local target_list=()
  local input=

  [[ -z "$target" ]] && \
    { _perr "${FUNCNAME}: target is empty"; return 1; }

  local lang_dir=
  for lang_dir in $(_get_lang_dirs)
  do
    target_full="${PAGES_DIR:?}/$lang_dir/$target"
    if [[ -e "$target_full" ]] && [[ ! -d "$target_full" ]]
    then
      _perr "'$target_full' should be a directory, but isn't."
      return 1
    fi
    target_list+=("$target_full")
  done
  unset lang_dir

  local t=
  for t in "${target_list[@]}"
  do
    if [[ ! -e "$t" ]]
    then
      _perr "Skipping non-existent directory '$t'."
    else
      rm -rf -- "$t" || return 1  # Provide error message?
      printf '%s\n' "Removed page '$t'."
    fi
  done
  unset t
}

_del_page_nav() {

  local grep_result=

  grep -q -- "^[[:space:]]*${target}/[[:space:]]*$" "$NAVTREE_FILE"
  grep_result="$?"

  if [[ "$grep_result" -gt 1 ]]  # grep(1) uses return values > 1 for errors.
  then
    _perr "Could not look up '$target' in '$NAVTREE_FILE'."
    return 1
  elif [[ "$grep_result" -eq 0 ]]
  then
    { cp -a -- "$NAVTREE_FILE" "${NAVTREE_FILE}.old" && \
      sed -- "\,^[[:space:]]*${target}/[[:space:]]*\$, d" \
      "${NAVTREE_FILE}.old" > "$NAVTREE_FILE"; } || \
      { _perr "Could not remove '$target' from site navigation."; \
        return 1; }
    printf '%s\n' "Removed '$target' from site navigation."
  fi
}

_show_help() {

  case "$1" in
#    _prompt_target)
#      :
#    ;;
    global)
cat <<'EOF'
Remove a page interactively

Usage:
    delpage

Options:
    --help
        Show this help text and exit
EOF
    ;;
    *)
      printf '%s\n' "Sorry, no help text available."
    ;;
  esac
}


#### MAIN ####

if [[ "$1" = '--help' ]]
then
  _show_help global
  exit
fi

## Environment checks

_env_checks || _abort

## Action

_get_pagetree
_prompt_target
if _prompt_confirm
then
  _del_page || { _perr "Aborted."; _abort; }
  _del_page_nav || _abort
fi
